package com.nowcoder.work.community.controller;

import com.nowcoder.work.community.annotation.LoginRequired;
import com.nowcoder.work.community.entity.Comment;
import com.nowcoder.work.community.entity.DiscussPost;
import com.nowcoder.work.community.entity.Page;
import com.nowcoder.work.community.entity.User;
import com.nowcoder.work.community.service.*;
import com.nowcoder.work.community.util.CommunityConstant;
import com.nowcoder.work.community.util.CommunityUtil;
import com.nowcoder.work.community.util.CosUtil;
import com.nowcoder.work.community.util.HostHolder;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController implements CommunityConstant {

    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Value("${community.path.upload}")
    private String uploadpath;

    @Value("${community.path.domain}")
    private String domain;

    @Value("${server.servlet.context-path}")
    private String contextPath;

    @Autowired
    private UserService userService;

    @Autowired
    private HostHolder hostHolder;

    @Autowired
    private LikeService likeService;

    @Autowired
    private FollowService followService;

    @Autowired
    private DiscussPostService discussPostService;

    @Autowired
    private CommentService commentService;


    @LoginRequired
    @RequestMapping(path = "/setting", method = RequestMethod.GET)
    public String getSettingPage() {
        return "site/setting";
    }

    // 更新用户头像
    @RequestMapping(path = "/header/url", method = RequestMethod.POST)
    public String updateHeaderUrl(MultipartFile file) {
        System.out.println("====上传图片====");
        String url = null;
        try {
            if (file == null) {
                return CommunityUtil.getJSONString(1, "文件名不能为空！");
            }
            url = CosUtil.uploadFile(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        userService.updateHeader(hostHolder.getUser().getId(), url);
        return "redirect:user/setting";
    }

    /**
     * 修改用户的密码
     */
    @LoginRequired
    @RequestMapping(path = "/updatepwd", method = RequestMethod.POST)
    public String updatePwssword(String oldPassword, String newPassword, Model model) {

        Map<String, Object> map = userService.updatePassword(oldPassword, newPassword);
        if (map != null) {
            model.addAttribute("oldPasswordMsg", map.get("oldPasswordMsg"));
            model.addAttribute("newPasswordMsg", map.get("newPasswordMsg"));
            return "site/setting";
        } else {
            model.addAttribute("msg", "您的密码已经修改成功，请重新登录！");
            String target = domain + contextPath + "/login";
            model.addAttribute("target", target);
            return "site/operate-result";
        }
    }

    // 个人主页
    @RequestMapping(path = "/profile/{userId}", method = RequestMethod.GET)
    public String getProfilePage(@PathVariable("userId") int userId, Model model) {
        User user = userService.findUserById(userId);
        if (user == null) {
            throw new RuntimeException("该用户不存在！");
        }
        // 用户
        model.addAttribute("user", user);

        // 关注数量
        long followeeCount = followService.findFolloweeCount(userId, ENTITY_TYPE_USER);
        model.addAttribute("followeeCount", followeeCount);
        // 粉丝数量
        long followerCount = followService.findFollowerCount(ENTITY_TYPE_USER, userId);
        model.addAttribute("followerCount", followerCount);

        // 是否已关注
        boolean hasFollowed = false;
        if (hostHolder.getUser() != null) {
            hasFollowed = followService.hasFollwed(hostHolder.getUser().getId(), ENTITY_TYPE_USER, userId);
        }
        model.addAttribute("hasFollowed", hasFollowed);


        // 获赞数量
        int likeCount = likeService.findUserLikeCount(userId);
        model.addAttribute("likeCount", likeCount);
        model.addAttribute("nowUserId", userId);
        return "/site/profile";
    }

    // 查看我的帖子
    @RequestMapping(path = "/my-post/{nowUserId}", method = RequestMethod.GET)
    public String getMyPost(@PathVariable("nowUserId") int nowUserId, Model model, Page page) {
        // 方法调用前，SpringMVC会自动实例化MOdel和page, 并将Page注入Model
        // 所以，在thymeleaf中可以直接访问Page对象中的数据
        //User user = hostHolder.getUser();
        page.setRows(discussPostService.findDiscussPostsRows(nowUserId));
        page.setPath("/user/my-post/" + nowUserId);

        List<DiscussPost> list = discussPostService.findDiscussPosts(nowUserId, page.getOffset(), page.getLimit(), 0);
        List<Map<String, Object>> discussPosts = new ArrayList<>();
        for (DiscussPost post : list) {
            Map<String, Object> map = new HashMap<>();
            map.put("post", post);

            long likeCount = likeService.findEntityLikeCount(ENTITY_TYPE_POST, post.getId());
            map.put("likeCount", likeCount);

            discussPosts.add(map);
        }
        model.addAttribute("discussPosts", discussPosts);
        return "site/my-post";
    }

    // 查看我的回复
    // /my-reply/xxx
    @RequestMapping(path = "/my-reply/{nowUserId}", method = RequestMethod.GET)
    public String getMyReply(@PathVariable("nowUserId") int nowUserId, Model model, Page page) {
        // 方法调用前，SpringMVC会自动实例化MOdel和page, 并将Page注入Model
        // 所以，在thymeleaf中可以直接访问Page对象中的数据

        page.setRows(discussPostService.findDiscussPostsRows(nowUserId));
        page.setPath("/user/my-reply/" + nowUserId);

        List<Comment> list = commentService.findCommentsOfUser(nowUserId, page.getOffset(), page.getLimit());
        List<Map<String, Object>> comments = new ArrayList<>();
        for (Comment comment : list) {
            Map<String, Object> map = new HashMap<>();
            //System.out.println(comment);
            // 判断这个评论回复的是帖子还是评论
            // 如果是评论，需要再次查找，找到对应的帖子，方便跳转
            if (comment.getEntityType() == ENTITY_TYPE_POST) {
                // 查找帖子
                int postId = comment.getEntityId();
                DiscussPost post = discussPostService.findDiscussPostById(postId);
                if (post.getStatus() == 2) {  // 如果帖子被删除了，直接跳过
                    continue;
                }
                map.put("postId", postId);
                map.put("post01", post);
                map.put("comment01", null);
                //System.out.println("post01>>>>>>>>>>>>>>>>>>>>>>");
            } else if (comment.getEntityType() == ENTITY_TYPE_COMMENT) {
                // 查找回复的comment
                Comment comment1 = commentService.findCommentById(comment.getEntityId());
                int postId = comment1.getEntityId();
                DiscussPost post = discussPostService.findDiscussPostById(postId);
                if (post.getStatus() == 2) {  // 如果帖子被删除了，直接跳过
                    continue;
                }
                map.put("postId", postId);
                map.put("comment01", comment1);
                map.put("post01", null);
                //System.out.println("comment01>>>>>>>>>>>>>>>>>>>>>>>>>");
            }

            map.put("comment", comment);

            comments.add(map);
        }
        model.addAttribute("nowUserId", nowUserId);
        model.addAttribute("comments", comments);
        return "site/my-reply";
    }
}
